<?php

namespace backend\models;

use Yii;
use backend\models\Members;

class Queues extends \common\models\Queues
{
    public $realName;
    public $idCardNo;
    

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'queueId' => Yii::t('app', 'Queue ID'),
            'queueKey' => Yii::t('app', 'Queue Key'),
            'realName' => Yii::t('app', 'Real Name'),
            'idCardNo' => Yii::t('app', 'Id Card No'),
            'memberKey' => Yii::t('app', 'Member Key'),
            'queueNo' => Yii::t('app', 'Queue No'),
            'inData' => Yii::t('app', 'In Data'),
            'queueNumber' => Yii::t('app', 'Queue Number'),
            'status' => Yii::t('app', 'Status'),
            'call' => Yii::t('app', 'Call'),
            'ctime' => Yii::t('app', 'Ctime'),
        ];
    }

    /**
     * 时间段（日）排队总人次数
     */
    public static function todayQueueNumber($days){
        $data = static::find()->select('count(1) as total')->where(['inData' => $days])->asArray()->one();
        return $data['total'];
    }

    /**
     * 时间段（日）排队人次数（等待叫号）
     */
    public static function currentQueueNumber($days){
        $data = static::find()->select('count(1) as total')->where(['inData' => $days, 'status' => 1, 'call' => 1])->asArray()->one();
        return $data['total'];
    }

    /**
     * 时间段（日）已叫号人次数
     */
    public static function todayCallNumber($days){
        $data = static::find()->select('count(1) as total')->where(['inData' => $days, 'status' => [1,3], 'call' => [2,3]])->asArray()->one();
        return $data['total'];
    }

    /**
     * 时间段（日）今日过号人次数
     */
    public static function todayPassNumber($days){
        $data = static::find()->select('count(1) as total')->where(['inData' => $days, 'status' => 3, 'call' => 3])->asArray()->one();
        return $data['total'];
    }

    /**
     * 时间段（日）今日进场（核销）人次数
     */
    public static function todayInNumber($days){
        $data = static::find()->select('count(1) as total')->where(['inData' => $days, 'status' => 2])->asArray()->one();
        return $data['total'];
    }

    /**
     * 计算指定日期端的排队人数统计
     */
    public static function dayQueueStat($section){
        $data = static::find()->select('count(1) as total, inData')->where(['inData' => $section])->groupBy('inData')->asArray()->all();
        $ds = [];
        foreach($data as $k => $v){
            $ds[$v['inData']] = intval($v['total']);
        }
        $d = [];
        foreach((array)$section as $k => $v){
            $d[] = isset($ds[$v]) ? $ds[$v] : 0;
        }
        return $d;
    }

    /**
     * 计算指定日期端的进场（核销）人次数统计
     */
    public static function dayInStat($section){
        $data = static::find()->select('count(1) as total, inData')->where(['inData' => $section, 'status' => 2])->groupBy('inData')->asArray()->all();
        $ds = [];
        foreach($data as $k => $v){
            $ds[$v['inData']] = intval($v['total']);
        }
        $d = [];
        foreach((array)$section as $k => $v){
            $d[] = isset($ds[$v]) ? $ds[$v] : 0;
        }
        return $d;
    }

    /**
     * 计算指定日期端的进场（核销）人次数统计
     */
    public static function dayPassStat($section){
        $data = static::find()->select('count(1) as total, inData')->where(['inData' => $section, 'status' => 3, 'call' => 3])->groupBy('inData')->asArray()->all();
        $ds = [];
        foreach($data as $k => $v){
            $ds[$v['inData']] = intval($v['total']);
        }
        $d = [];
        foreach((array)$section as $k => $v){
            $d[] = isset($ds[$v]) ? $ds[$v] : 0;
        }
        return $d;
    }

    public static function QueueStat($section){
        $d = [];
        foreach($section as $k => $sec){
            $data = static::find()->select('count(1) as total')->where(['inData' => $sec])->asArray()->one();
            $d[] = $data['total'];
        }
        return $d;
    }

    public static function InStat($section){
        $d = [];
        foreach($section as $k => $sec){
            $data = static::find()->select('count(1) as total')->where(['inData' => $sec, 'status' => 2])->asArray()->one();
            $d[] = $data['total'];
        }
        return $d;
    }

    public static function PassStat($section){
        $d = [];
        foreach($section as $k => $sec){
            $data = static::find()->select('count(1) as total')->where(['inData' => $sec, 'status' => 3, 'call' => 3])->asArray()->one();
            $d[] = $data['total'];
        }
        return $d;
    }

    public static function yearQueueStat($section){
        $d = [];
        foreach($section as $k => $year){
            $sec = static::getYearStartEnd($year);
            $data = static::find()->select('count(1) as total')->where(['>=', 'ctime', $sec['start']])->andWhere(['<', 'ctime', $sec['end']])->asArray()->one();
            $d[] = $data['total'];
        }
        return $d;
    }

    public static function yearInStat($section){
        $d = [];
        foreach($section as $k => $year){
            $sec = static::getYearStartEnd($year);
            $data = static::find()->select('count(1) as total')->where(['>=', 'ctime', $sec['start']])->andWhere(['<', 'ctime', $sec['end']])->andWhere(['status' => 2])->asArray()->one();
            $d[] = $data['total'];
        }
        return $d;
    }

    public static function yearPassStat($section){
        $d = [];
        foreach($section as $k => $year){
            $sec = static::getYearStartEnd($year);
            $data = static::find()->select('count(1) as total')->where(['status' => 3])->andWhere(['call' => 3])->andWhere(['<', 'ctime', $sec['end']])->andWhere(['>=', 'ctime', $sec['start']])->asArray()->one();
            $d[] = $data['total'];
        }
        return $d;
    }

    private static function getYearStartEnd($year){
        $start = strtotime($year.'-1-1 00:00:00');
        $end = strtotime(($year + 1).'-1-1 00:00:00');
        return ['start' => $start, 'end' => $end];
    }
}
